package com.mindai.esper.monitor.biz.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.mindai.esper.monitor.biz.constant.Configuration;

import java.sql.*;

/**
 * Created by Ladd on 16-4-15.
 */
public class DBConnectionUtils {

    private static DruidDataSource dataSource = null;

    static {
        dataSource = new DruidDataSource();
        dataSource.setDriverClassName(Configuration.getConfig().getString("rtc.drivename"));
        dataSource.setUsername(Configuration.getConfig().getString("rtc.username"));
        dataSource.setPassword(Configuration.getConfig().getString("rtc.password"));
        dataSource.setUrl(Configuration.getConfig().getString("rtc.url"));
        dataSource.setInitialSize(5);
        dataSource.setMinIdle(1);
        dataSource.setMaxActive(10);
        dataSource.setValidationQuery("select 1 from dual");
    }

    public static DruidPooledConnection getConnection() {
        DruidPooledConnection con = null;
        try {
            con = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }
}
